package cn.com.qtgl.apis.iam.comp.service;

import cn.com.qtgl.apis.builtin.model.vo.*;
import cn.com.qtgl.apis.iam.comp.proxy.IamProxy;
import cn.com.qtgl.apis.iam.model.vo.DynamicValue;
import cn.com.qtgl.apis.iam.model.vo.Position;
import cn.com.qtgl.apis.iam.model.vo.*;
import cn.melonlib.authentication.model.entity.Tenant;
import cn.melonlib.cores.commons.comp.service.impl.WorkerBasicService;
import cn.melonlib.cores.commons.comp.utils.FilterTools;
import cn.melonlib.cores.graphql.comp.utils.GraphQLTools;
import cn.melonlib.cores.jpa.model.bean.impl.PaginationList;
import cn.melonlib.cores.model.anno.*;
import cn.melonlib.cores.model.input.OrderByInput;
import cn.melonlib.objects.maps.JsonMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;

import static cn.melonlib.cores.commons.comp.service.impl.WorkerBasicService.ROOT_METHOD.*;

@Service
@WorkerService(
        module = "apis",
        model = @WorkerModel(
                modelType = User.class
                ,extendsTypes = {
                ApiPermission.class, Certificate.class, Department.class,DynamicValue.class
                ,Position.class, Post.class, Tenant.class
        }
        )
        ,ignores = {LOAD_ALL,LIST,SAVE,REMOVE_BY_ID,REMOVE_BY_ID_LIST,TOTAL_COUNT,GET_BY_ID}
)
@WorkerComment("用户信息")
public class UserService extends WorkerBasicService<User> {

        @Resource
        private GraphQLTools graphQLTools;

        @Resource
        private IamCacheService iamCacheService;

        @Resource
        private IamProxy iam;

        @Resource
        private FilterTools filterTools;

        @Override
        @WorkerQuery
        @WorkerComment("查询用户信息")
        public <DATA extends PaginationList<User>> DATA search(
                @WorkerParameter(value = "first",comment = "数量",required = true) Integer first,
                @WorkerParameter(value = "offset",comment = "偏移量",required = true) Long offset,
                @WorkerParameter(value = "filter",comment = "过滤",required = true) JsonMap<JsonMap<Object>> filter,
                @WorkerParameter(value = "orderBy",comment = "排序") OrderByInput orderBy
        ) throws Exception {
                IamApiResult<UserEdges> users=iamCacheService.users();
                return (DATA) PaginationList.createOffset(users.getEdges().stream().map(UserEdges::getNode).filter(node-> graphQLTools.filter(node,filter)).skip(offset).limit(first)
                        .collect(Collectors.toList()),0,users.getEdges().size(),users.getTotalCount());
        }

        @WorkerQuery
        @WorkerComment("获取用户")
        public User getUser(@WorkerParameter(value = "account",comment = "用户账户",required = true) String account){
                try {
                        return iam.users(filterTools.createFilters().addEq("account", account).getConditionsMap()).getEdges().get(0).getNode();
                }catch (Exception e){
                        return null;
                }
        }
        @WorkerQuery
        @WorkerComment("获取当前用户")
        public User getCurUser(){
                try {
                        return iam.users(filterTools.createFilters().addEq("account", getIdentificationMessenger().getIdentification().getAccount())
                                .getConditionsMap()).getEdges().get(0).getNode();
                }catch (Exception e){
                        return null;
                }
        }


}
